package com.wzh.musichome.controller;


import com.wzh.musichome.common.ConstantCode;
import com.wzh.musichome.common.ResponseTemplate;
import com.wzh.musichome.entity.dto.ScoreDTO;
import com.wzh.musichome.entity.pojo.Score;
import com.wzh.musichome.service.ScoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 评价 前端控制器
 * </p>
 *
 * @author wzh
 * @since 2020-12-10
 */
@RestController
public class ScoreController {

    @Autowired
    private ScoreService scoreService;


    @PostMapping("/score/add")
    public Object add(@RequestBody ScoreDTO scoreDTO){
        Score score = new Score();
        BeanUtils.copyProperties(scoreDTO,score);
        try {
            boolean flag = scoreService.save(score);
            if (flag) {
                ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
                data.setMsg("评价成功");
                return data;
            }
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_INSERT, "评价失败！" );
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_INSERT, "评价失败！！ 具体原因" + e.getMessage());
        }
    }

    /**
     * 计算平均分
     */
    @GetMapping("/score/{songListId}")
    public Object scoreOfSongListId(@PathVariable int songListId){
        try {
           int i = scoreService.scoreOfSongListId(songListId);
            ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
            data.setMsg("评价成功");
            data.setData(i);
            return data;
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_QUERY, "获取评分失败！！ 具体原因" + e.getMessage());
        }
    }
}
